package com.htjc.integral.common.util;

import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zw
 */
public class SensitiveDataUtil {
    private static final int SIZE = 4;
    private static final String SYMBOL = "*";

    public static String toConceal(String value) {
        if (null == value || "".equals(value)) {
            return value;
        }
        int len = value.length();
        int pamaone = len / 2;
        int pamatwo = pamaone - 1;
        int pamathree = len % 2;
        StringBuilder stringBuilder = new StringBuilder();
        if (len <= 2) {
            System.out.println("aaaaaaaaaaaaaaaaaa");
            if (pamathree == 1) {
                return SYMBOL;
            }
            stringBuilder.append(SYMBOL);
            stringBuilder.append(value.charAt(len - 1));
        } else {
            if (pamatwo <= 0) {
                System.out.println("bbbbbbbbbbbbbbb");
//                stringBuilder.append(value.substring(1,len));
                stringBuilder.append(SYMBOL);
                stringBuilder.append(value.substring(len -2, len));

            }else if(pamatwo == 1||pamatwo == 2){
                System.out.println("dsdsfddsfd");
//               stringBuilder.append(value.substring(0, len));
                stringBuilder.append(SYMBOL);
                stringBuilder.append(SYMBOL);
                stringBuilder.append(value.substring(len -(len-2), len));
            }
            else if (pamatwo >= SIZE / 2 && SIZE + 1 != len) {
                System.out.println("cccccccccccccc");
                int pamafive = (len - SIZE) / 2;
                if ((pamathree == 0 && SIZE / 2 == 0) || (pamathree != 0 && SIZE % 2 != 0)) {
                    System.out.println("ddddddddddddddddddd");
                    stringBuilder.append(value.substring(0, 2));
                    for (int i = 0; i < SIZE; i++) {
                        stringBuilder.append(SYMBOL);
                    }
                    stringBuilder.append(value.substring(len - pamafive, len));
                } else {
                    System.out.println("eeeeeeeeeeeeee");
                    stringBuilder.append(value.substring(0, 3));
                    for (int i = 0; i < SIZE; i++) {
                        stringBuilder.append(SYMBOL);
                    }
                    stringBuilder.append(value.substring(len - pamafive, len));
                }
            } else {
                System.out.println("ffffffffffff");
                int pamafour = len - 2;
                stringBuilder.append(value.substring(0, 1));
                for (int i = 0; i < pamafour; i++) {
                    stringBuilder.append(SYMBOL);
                }
                stringBuilder.append(value.substring(len - 1, len));
            }
        }
        return stringBuilder.toString();

    }

    /**
     * 脱敏工具测试函数
     *
     * @param args
     */
    public static void main(String[] args) {
//        如为手机号（11位，且全为数字）：保留前3位和最后3位，其余用*代替。
//        如不为手机号的，应用一下规则：
//        3个字以内隐藏第1个字，
//        4-6个字隐藏前2个字，
//        大于6个字隐藏第3-6个字，隐藏字用*代替。
        System.out.println("++++++++++++++++++++" + toConceal("1303965799"));
        System.out.println("++++++++++++++++++++" + toConceal("张伟"));
        System.out.println("++++++++++++++++++++" + toConceal("梁小猪"));
        System.out.println("++++++++++++++++++++" + toConceal("张加加加"));
        System.out.println("++++++++++++++++++++" + toConceal("张加加加看看"));
        System.out.println("++++++++++++++++++++" + toConceal("张伟sdasdfadfsadas萨德"));
        System.out.println("++++++++++++++++++++" + toConceal("873588295@qq.com"));
    }
}
